/**
 * 
 */
package be.bejug.scrumadmin.spring;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.vaadin.ui.Component;

/**
 * @author Glenn Dejaeger
 * 
 */
public class Injector {

	public static void inject(final Component component) {
		inject(component, getApplicationContext());
	}

	private static ApplicationContext getApplicationContext() {
		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
				.currentRequestAttributes();
		HttpServletRequest request = requestAttributes.getRequest();
		HttpSession session = request.getSession(false);
		return WebApplicationContextUtils
				.getRequiredWebApplicationContext(session.getServletContext());
	}

	private static void inject(final Component component,
			final ApplicationContext applicationContext) {
		AutowireCapableBeanFactory beanFactory = applicationContext
				.getAutowireCapableBeanFactory();
		beanFactory.autowireBeanProperties(component,
				AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, false);
	}
}
